<?php
/**
 * Description of APi 接收手机数据
 * @author l
 */
namespace Home\Controller;
use Think\Controller;

class PhoneController extends Controller {

    public function __construct() {
    }

    function buling($num,$len=4){
        $str_len = strlen($num);
        switch($str_len){
            case 1:
                $num = '000'.$num;
                break;
            case 2:
                $num = '00'.$num;
                break;
            case 3:
                $num = '0'.$num;
                break;
        }
        return (string)$num;
    }

    public function index() {
        //echo strlen('');exit;
//        $res = $this->buling('31133');
//        _debug($res);
//        echo substr('13620480009',4,4);exit;
//        $num = 479;
//        echo $num+1;
//        echo strpos('13620480009',(string)($num+1));exit;
//        if((strpos(480,'13620480009'))){
//            output([],201,'数据异常!');
//        }
//
//        exit;
        if(I('post.')&&trim(I('post.real_account'))){
            if(I('get.real')){
                $exist = M('alipay_account')->where(['account'=>I('post.account'),'name'=>I('post.name')])->find();
                if($exist){
                    $data = [
                        'account' => I('post.account'),
                        'name' => I('name'),
                        'num' => I('num'),
                        'real_account' => I('real_account'),
                        'real_name' => I('real_name'),
                        'status' => I('status')
                    ];
                    M('alipay_account')->where(['id'=>$exist['id']])->save($data);
                }else{
                    $data = [
                        'account' => I('post.account'),
                        'name' => I('name'),
                        'num' => I('num'),
                        'real_account' => I('real_account'),
                        'real_name' => I('real_name'),
                        'status' => I('status')
                    ];
                    M('alipay_account')->add($data);
                }

                $exist = M('alipay_account_test')->where(['real_account'=>I('post.real_account')])->find();
                if($exist){
                    //操作太频繁/已获取账号数据，禁止修改数据
                    if(I('post.status')==4||I('post.status')==1){
                        output();
                    }
                    $data = [
                        'account' => I('post.account'),
                        'name' => I('name'),
                        'num' => I('num'),
                        'real_account' => I('real_account'),
                        'real_name' => I('real_name'),
                        'content' => I('content'),
                        'status' => I('status'),
                        'operate' => I('operate'),
                    ];
                    M('alipay_account_test')->where(['id'=>$exist['id']])->save($data);
                }else{
                    $data = [
                        'account' => I('post.account'),
                        'name' => I('name'),
                        'num' => I('num'),
                        'real_account' => I('real_account'),
                        'real_name' => I('real_name'),
                        'content' => I('content'),
                        'status' => I('status'),
                        'operate' => I('operate'),
                    ];
                    M('alipay_account_test')->add($data);
                }

            }else {
                $exist = M('alipay_account_test')->where(['real_account'=>I('post.real_account')])->find();
                if($exist){
                    //操作太频繁/已获取账号数据，禁止修改数据
                    if(I('post.status')==4||$exist['status']==1){
                        output();
                    }
                    $data = [
                        'account' => I('post.account'),
                        'name' => I('name'),
                        //'num' => I('num'),
                        'real_account' => I('real_account'),
                        'real_name' => I('real_name'),
                        'content' => I('content'),
                        'status' => I('status'),
                        'operate' => I('operate'),
                    ];
                    M('alipay_account_test')->where(['id'=>$exist['id']])->save($data);
                }else{
                    //检测前一条记录
                    $exist = M('alipay_account_test')->where([])->order('id desc')->limit(1)->find();
                    if($exist['real_name']&&$exist['real_name']==I('real_name')){
                        output([],201,'请求过快!');
                    }
                    //对比num和手机号码
                    if(strlen(I('num'))>0) {
                        $s = $this->buling(I('num') + 1);
                    }else{
                        $s = '0000';
                    }

                    if(!(strpos(substr(I('real_account'),4,4),$s)>-1)){
                        output([],201,'数据异常!');
                    }
                    $data = [
                        'account' => I('post.account'),
                        'name' => I('name'),
                        'num' => I('num'),
                        'real_account' => I('real_account'),
                        'real_name' => I('real_name'),
                        'content' => I('content'),
                        'status' => I('status'),
                        'operate' => I('operate'),
                    ];
                    M('alipay_account_test')->add($data);
                }
            }
            output();
        }
    }

    //领取任务v2  1359****042
    public function task2() {
        $account = I('post.account');
        //$account = '1362****009';
        //$account = '1359****042';

        $info = M('alipay_account')->where(['account'=>$account])->find();
        //_debug($info);
        // $data = [
        //     'status' => 1
        // ];
        // M('alipay_account')->where(['id'=>$info['id']])->save($data);//修改任务为领取状态
        if($info){
            $info_test = M('alipay_account_test')->where(['status'=>['neq',4],'account'=>$info['account'],'name'=>$info['name']])->order('id desc')->find();
            if($info_test){
                $info['num'] = strlen($info_test['num'])>0 ? $info_test['num']+1 : 0;
                $info['pre_real_name'] = $info_test['real_name'];
            }else{
                $info['num'] = '';
                $info['pre_real_name'] = '';
            }
            output($info,200,'领取任务成功');
        }else{
            output([],201,'暂无任务');
        }
    }

    //领取任务v1
    public function task() {
        $info = M('alipay_account')->where(['status'=>['lt',2]])->find();
        // $data = [
        //     'status' => 1
        // ];
        // M('alipay_account')->where(['id'=>$info['id']])->save($data);//修改任务为领取状态
        if($info){
            $info_test = M('alipay_account_test')->where(['status'=>['neq',4],'account'=>$info['account'],'name'=>$info['name']])->order('id desc')->find();
            if($info_test){
                $info_test['num'] = $info_test['num']>0 ? $info_test['num'] : 0;
                $info['num'] = $info_test['num']+1;
            }
            output($info,200,'领取任务成功');
        }else{
            output([],201,'暂无任务');
        }
    }
}
